using System.Data;
using System.Collections;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System;
using Microsoft.ReportingServices.ReportRendering;

namespace CustomExcelRenderCS
{
    public class ListRenderer
    {
        internal static int subreportTableNoRows;

        internal ListRenderer()
        {
        }
        internal string Render(Microsoft.ReportingServices.ReportRendering.List list)
        {
            System.Text.StringBuilder pHtml = new System.Text.StringBuilder();
            pHtml.AppendFormat("<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">");
            //pHtml.Append("<TR>");
            if (list.Contents == null)
            {
                pHtml.AppendFormat("<tr><td><div STYLE=\"WIDTH:{1}pt;HEIGHT:{0}pt\">", list.Height.ToPoints().ToString(), list.Width.ToPoints().ToString());
                pHtml.Append("&nbsp;");
                pHtml.Append("</div></td></tr>");
            }
            else
            {
                for (int k = 0; k < list.Contents.Count; k++)
                {
                    pHtml.Append("<tr>");

                    // get subreport table number of rows
                    for (int i = 0; i <= list.Contents[k].ReportItemCollection.Count - 1; i++)
                    {
                        ReportItem rItem = list.Contents[k].ReportItemCollection[i];
                        if (rItem.GetType() == typeof(SubReport))
                        {
                            ReportItemCollection rc = ((SubReport)rItem).Report.Body.ReportItemCollection;
                            for (int j = 0; j < rc.Count; j++)
                            {
                                if (rc[j].GetType() == typeof(Table))
                                {
                                    subreportTableNoRows = ((Table)rc[j]).DetailRows.Count;
                                }
                            }
                        }
                    }

                    // process each item in list now
                    for (int i = 0; i <= list.Contents[k].ReportItemCollection.Count - 1; i++)
                    {
                        ReportItem rItem = list.Contents[k].ReportItemCollection[i];
                        string tdDef = (rItem.GetType()== typeof(SubReport))?"":Renderer.getTDDef(rItem);
                        pHtml.AppendFormat(tdDef);
                        pHtml.Append(Renderer.RenderReportItem(rItem));
                        pHtml.Append("</td>");
                    }
                    pHtml.Append("</tr>");
                }

            }
            pHtml.Append("</TR>");
            pHtml.Append("</TABLE>\n");

            return pHtml.ToString();
        }
    }

}
